<?php

// @formatter:off
/**
 * @file edittagsof.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: edittagsof.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/info/edittagsof.php $
 */
// @formatter:on

class ActionControllerInfoEdittagsof extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;
			global $wcGroupForbidden;
			global $wcComic;
			global $wcImage;
			global $wcDirectory;
			global $wcTag;

			if($wcGroupForbidden) {
				$check = WC_HTTPERROR_FORBIDDEN;

				global $errno;
				global $error;

				$errno = $check;
				$error = "You're not allowed to access this group";
			}

			if($check == WC_HTTPERROR_OK && !$wcProfile->allowedAny(WC_PERM_COMIC_SET_TAG, WC_PERM_IMAGE_SET_TAG, WC_PERM_DIRECTORY_SET_TAG, WC_PERM_TAG_SET_TAG)) {
				$check = WC_HTTPERROR_FORBIDDEN;

				global $errno;
				global $error;

				$errno = $check;
				$error = "You're not allowed to edit any tags";
			}

			if($check == WC_HTTPERROR_OK && $wcComic) {
				if(!$wcProfile->granted(WC_PERM_COMIC_SET_TAG)) {
					global $errno;
					global $error;
					$check = WC_HTTPERROR_FORBIDDEN;
					$errno = $check;
					$error = "You're not allowed to edit comic's tags";
				} elseif(!$wcComic->ok()) {
					global $errno;
					global $error;
					$check = WC_HTTPERROR_BAD_REQUEST;
					$errno = $check;
					$error = "Specified comic is not ok";
				}
			} elseif($check == WC_HTTPERROR_OK && $wcImage) {
				if(!$wcProfile->granted(WC_PERM_IMAGE_SET_TAG)) {
					global $errno;
					global $error;
					$check = WC_HTTPERROR_FORBIDDEN;
					$errno = $check;
					$error = "You're not allowed to edit image's tags";
				} elseif(!$wcImage->ok()) {
					global $errno;
					global $error;
					$check = WC_HTTPERROR_BAD_REQUEST;
					$errno = $check;
					$error = "Specified image is not ok";
				}
			} elseif($check == WC_HTTPERROR_OK && $wcDirectory) {
				if(!$wcProfile->granted(WC_PERM_DIRECTORY_SET_TAG)) {
					global $errno;
					global $error;
					$check = WC_HTTPERROR_FORBIDDEN;
					$errno = $check;
					$error = "You're not allowed to edit directory's tags";
				} elseif(!$wcDirectory->ok()) {
					global $errno;
					global $error;
					$check = WC_HTTPERROR_BAD_REQUEST;
					$errno = $check;
					$error = "Specified directory is not ok";
				}
			} elseif($check == WC_HTTPERROR_OK && $wcTag) {
				if(!$wcProfile->granted(WC_PERM_TAG_SET_TAG)) {
					global $errno;
					global $error;
					$check = WC_HTTPERROR_FORBIDDEN;
					$errno = $check;
					$errno = $check;
					$error = "You're not allowed to edit tag's tags";
				} elseif(!$wcTag->ok()) {
					global $errno;
					global $error;
					$check = WC_HTTPERROR_BAD_REQUEST;
					$errno = $check;
					$error = "Specified tag is not ok";
				}
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcComic;
		global $wcImage;
		global $wcDirectory;
		global $wcTag;

		if($wcComic) {
			$this->_manager->edit_what = "Comic";
			$this->_manager->edit_name = $wcComic->name();
			$this->_manager->edit_id = $wcComic->id();
			$this->_manager->edit_uri_name = "comic";
			$this->_manager->edit_previews = array($wcComic->preview());
		} elseif($wcImage) {
			$this->_manager->edit_what = "Image";
			$this->_manager->edit_name = $wcImage->name();
			$this->_manager->edit_id = $wcImage->id();
			$this->_manager->edit_uri_name = "image";
			$this->_manager->edit_previews = array($wcImage->preview());
		} elseif($wcDirectory) {
			$this->_manager->edit_what = "Directory";
			$this->_manager->edit_name = $wcDirectory->name();
			$this->_manager->edit_id = $wcDirectory->id();
			$this->_manager->edit_uri_name = "directory";
			$this->_manager->edit_previews = $wcDirectory->previews();
		} elseif($wcTag) {
			$this->_manager->edit_what = "Tag";
			$this->_manager->edit_name = $wcTag->name();
			$this->_manager->edit_id = $wcTag->id();
			$this->_manager->edit_uri_name = "tag";
			$this->_manager->edit_previews = $wcTag->previews();
		}

		return $ok;
	}
}
